package engine.display.newAttack.action.effect
{
	import engine.logic.McPool;
	
	import flash.display.BlendMode;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;

	public class PointPlayLite
	{
		/**特效舞台*/
		protected var _space			:DisplayObjectContainer;
		
		/**产生点*/
		protected var _birthPoint		:Point;
		
		/**动画文件*/		
		private var _skin				:MovieClip;
		
		private var _effectMovie		:MovieClip;
		
		/**播放过程中触发*/
		protected var _onPlay			:Function	=	null;
		
		/**播放完成后播放*/
		protected var _onPlayOver		:Function	=	null;
		
		public function PointPlayLite(
								space		:DisplayObjectContainer,
								birthPoint	:Point,
								effectSkin	:MovieClip,
								obj			:Object		=	null,
								onPlay		:Function	=	null,
								onPlayOver	:Function	=	null)
		{
			if(null	==	effectSkin || effectSkin.movie == null )
			{//当没特效动画的时候，不播放特效
				if(onPlayOver!=null)
				{
					onPlayOver.call(this,this);
				}
				return;
			}
			
			_space		=	space;
			_birthPoint	=	birthPoint;
			_skin		=	effectSkin;
			_effectMovie=	_skin.movie;
			_onPlay		=	onPlay;
			_onPlayOver	=	onPlayOver;
			
			createEffect()
		}
		
		/**
		 * 生成特效
		 */		
		private function createEffect():void
		{
			_skin.addEventListener(Event.ADDED_TO_STAGE,onSkinAdd);
			//_skin.blendMode	=	BlendMode.ADD;
			_space.addChild(_skin);
		}
		
		private function onSkinAdd(e:Event):void
		{
			_skin.removeEventListener(Event.ADDED_TO_STAGE,onSkinAdd);
			_effectMovie.gotoAndPlay( 1 );
			
			_space.setChildIndex(_skin,_space.numChildren-1);
			
			_skin.x	=	_birthPoint.x;
			_skin.y	=	_birthPoint.y;
			
			_skin.addEventListener(Event.ENTER_FRAME,onEffectPlay);
		}
		
		public function removeOnPlay():void
		{
			_onPlay	=	null;
		}
		
		protected function onEffectPlay(e:Event):void
		{
			if(_onPlay!=null)
			{
				_onPlay.call(this,this);
			}
			
			
			if( _effectMovie.currentFrame == _effectMovie.totalFrames )
			{
				onEffectPlayOver();
			}
		}
		
		protected function onEffectPlayOver():void
		{
			_skin.removeEventListener(Event.ENTER_FRAME,onEffectPlay);
			
			clearEffect();
			
			if(_onPlayOver!=null)
			{			
				_onPlayOver.call(this,this);
			}
		}
		
		/**
		 * 强行中断特效 
		 * 
		 */		
		public function KillEffect():void
		{
			_skin.removeEventListener(Event.ENTER_FRAME,onEffectPlay);
			
			clearEffect();
		}
				
		/**
		 * 特效播放结束
		 */		
		public function clearEffect():void
		{			
			_effectMovie.stop();
			
			if(_skin.parent)
			{
				_skin.parent.removeChild( _skin );
			}
			
			_space = null;
			
			McPool.returnRes(_skin["url"], _skin);
		}		

		/**实际动画**/
		public function get effectMovie():MovieClip
		{
			return _effectMovie;
		}

	}
}